package com.yingxin.yxerp.module.sys.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yingxin.yx.framework.commons.constant.CommonConst;
import com.yingxin.yx.framework.commons.exception.BizException;
import com.yingxin.yx.framework.user.core.IUserSupport;
import com.yingxin.yxerp.module.sys.entity.MaterialName;
import com.yingxin.yxerp.module.sys.entity.WasteName;
import com.yingxin.yxerp.module.sys.mapper.MaterialNameMapper;
import com.yingxin.yxerp.module.sys.mapper.WasteNameMapper;
import com.yingxin.yxerp.module.sys.service.IMaterialNameService;
import com.yingxin.yxerp.module.sys.vo.req.MaterialNameQueryVo;
import com.yingxin.yxerp.module.sys.vo.req.WasteNameQueryVo;
import com.yingxin.yxerp.module.sys.vo.resp.MaterialNameRespVo;
import com.yingxin.yxerp.module.sys.vo.resp.WasteNameRespVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 主料名称 服务类
 * @author yuebao
 * @since 2025-03-31
 */

@Service
public class MaterialNameServiceImpl
        extends ServiceImpl<MaterialNameMapper, MaterialName>
        implements IMaterialNameService {

    @Autowired
    private IUserSupport userSupport;

    /**
     * 获取 mapper
     */
    @Override
    public MaterialNameMapper mapper(){
        return baseMapper;
    }

    /**
     * 创建
     */
    @Override
    public void create(MaterialName materialName) {
        // 验证name是否重复
        checkRepeat( CommonConst.FALSE, materialName );

        materialName.setCreatorId( userSupport.getUserId() );
        materialName.setCreatorName( userSupport.getStaffName() );
        materialName.setDelFlag( CommonConst.FALSE );

        save( materialName );
    }

    /**
     * 删除
     */
    @Override
    public void delete(String id) {
        MaterialName materialName = getById( id );
        materialName.setDelFlag( CommonConst.TRUE );
        updateById( materialName );
    }

    /**
     * 修改
     */
    @Override
    public void update(MaterialName materialName) {
        // 验证name是否重复
        checkRepeat( CommonConst.TRUE, materialName );
        MaterialName other = getById( materialName.getId() );
        other.setName( materialName.getName() );
        updateById( other );
    }

    /**
     * 验证对应的type 下的名称是否已经存在
     * @param addOrUpdate 0 = add， 1 = update
     * @param set
     */
    private void checkRepeat(String addOrUpdate, MaterialName materialName){
        QueryWrapper<MaterialName> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq( MaterialName::getDelFlag, CommonConst.FALSE );
        queryWrapper.lambda().eq( MaterialName::getName, materialName.getName() );
        if( CommonConst.TRUE.trim().equals( addOrUpdate ) ){
            queryWrapper.lambda().ne( MaterialName::getId, materialName.getId() );
        }
        List<MaterialName> lists = list( queryWrapper );
        if( CollectionUtil.isNotEmpty( lists ) ){
            throw new BizException("名称已经存在！");
        }
    }

    /**
     * 查询全部
     */
    @Override
    public List<MaterialNameRespVo> listVo(MaterialNameQueryVo queryVo){
        return baseMapper.listVo(queryVo);
    }

    /**
     * 根据ID查询
     */
    @Override
    public MaterialNameRespVo getVo(String id){
        return baseMapper.getVo( id );
    }

}


